﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace YiSha.Util
{
    /// <summary>
    /// CPU监控
    /// </summary>
    public sealed class CPUMonitor
    {
        private static readonly CPUMonitor instance = new CPUMonitor();
        private PerformanceCounter pcCpuLoad;
        private CPUMonitor()
        {
            //初始化CPU计数器
            pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            pcCpuLoad.MachineName = ".";
            pcCpuLoad.NextValue();
            System.Threading.Thread.Sleep(1000);
        }
        /// <summary>
        /// 获取监控信息
        /// </summary>
        /// <returns></returns>
        public static CPUMonitor getMonitor()
        {
            return instance;
        }
        public static float getValue()
        {
            return instance.pcCpuLoad.NextValue();
        }
    }
    /// <summary>
    /// 获取CPU核数
    /// </summary>
    public sealed class ProcessorCountMonitor
    {
        public static readonly ProcessorCountMonitor instance = new ProcessorCountMonitor();
        private int m_ProcessorCount = 0;   //CPU个数
        private ProcessorCountMonitor()
        {
            //CPU个数
            m_ProcessorCount = Environment.ProcessorCount;
        }
        public static ProcessorCountMonitor getMonitor()
        {
            return instance;
        }
        public static int getValue()
        {
            return getMonitor().m_ProcessorCount;
        }
    }
}
